package com.xxg.renrenuser.manager;

import com.alibaba.fastjson.JSON;
import com.xxg.renrencommon.exception.ErrorMessage;
import com.xxg.renrencommon.exception.UserManagerException;
import com.xxg.renrenuser.domain.result.BankAuthenticateResult;
import com.xxg.renrenuser.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云银行卡认证
 *
 * @author xxg
 * @date 2020/11/6
 */
@Component
public class AliCloudApi {

    private static final Logger logger = LoggerFactory.getLogger(AliCloudApi.class);
    @Value(value = "${aliyunapi.host}")
    private String host;
    @Value(value = "${aliyunapi.path}")
    private String path;
    @Value(value = "${aliyunapi.appcode}")
    private String appCode;

    /**
     * 三要素绑定银行卡
     *
     * @param cardNo 银行卡号
     * @param idNo   身份证号
     * @param name   姓名
     * @return 结果
     * @throws UserManagerException 异常信息
     */
    public BankAuthenticateResult bankAuthenticate3(String cardNo, String idNo, String name) throws UserManagerException {
        Map<String, String> headers = new HashMap<String, String>();
        // 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        // 根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("cardNo", cardNo);
        bodys.put("idNo", idNo);
        bodys.put("name", name);
        String method = "post";
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            BankAuthenticateResult result = JSON.parseObject(EntityUtils.toString(response.getEntity()), BankAuthenticateResult.class);
            if ("0000".equals(result.getRespCode())) {
                return result;
            } else {
                throw new UserManagerException(result.getRespMessage());
            }
        } catch (Exception e) {
            logger.error(ErrorMessage.BANK_AUTHENTICATE_ERROR + "\n接口:" + host + path + "\n参数:"
                    + JSON.toJSONString(bodys) + "\n错误信息:" + e.getMessage(), e);
            throw new UserManagerException(ErrorMessage.BANK_AUTHENTICATE_ERROR);
        }
    }
}
